package com.ruoyi.learn.java.thread;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.RandomUtils;

@Slf4j
public class WaitAndSllep {

    public static void main(String[] args) throws InterruptedException {
        // sleep() 示例
        Thread.sleep(1000); // 暂停1秒

// wait() 示例
        Object lockObject = new Object();
        new Thread(() -> {
            synchronized (lockObject) {
                try {
                    log.info("线程{}开始执行", Thread.currentThread().getName());
                    lockObject.wait(); // 释放锁并等待
                    // 条件满足后继续执行
                    log.info("线程{}继续执行", Thread.currentThread().getName());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();

        new Thread(() -> {
            synchronized (lockObject) {
                log.info("线程{}开始执行notify1", Thread.currentThread().getName());

                try {
                    Thread.sleep(RandomUtils.nextLong(1000, 5000));
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                log.info("线程{}开始执行notify2", Thread.currentThread().getName());

                lockObject.notify(); // 释放锁并等待
                // 条件满足后继续执行
                log.info("线程{}继续执行notify", Thread.currentThread().getName());
            }
        }).start();

    }
}
